package admin.drink.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.Drink;
import net.proteanit.sql.DbUtils;
import admin.drink.model.DrinkModel;

public class DrinkView extends JPanel {
	private static final long serialVersionUID = 1L;
	private final String NEW = "New";
	private final String UPDATE = "Update";
	private final String DELETE = "Delete";
	private final String SAVE = "Save";
	private final String CANCEL = "Cancel";

	private JTable tableDrink = null;
	private DrinkModel drinkModel;

	private JPanel buttonPanel;
	private JPanel textPanel;
	private JPanel tablePanel;

	private boolean b = false;// boolean value for enable disable Components
								// function
	private boolean isUpdateButton = false;// variable for save button action
	private boolean isNewButton = false;// variable for save button action
	private int dId = 0;// drinkId

	private JButton newB;
	private JButton updateB;
	private JButton deleteB;
	private JButton saveB;
	private JButton cancelB;

	private JTextField drinkNameTF;
	private JTextField drinkIdTF;
	private JTextField drinkDescTF;
	private JTextField drinkPriceTF;

	private ButtonActionListener buttonActionListener;
	private TableMouseAdapter tableMouseAdapter;

	// constructor
	public DrinkView() {
		buttonActionListener = new ButtonActionListener();
		tableMouseAdapter = new TableMouseAdapter();
		drinkModel = new DrinkModel();
		initConponents();
		update();
	}

	public void update() {
		updateView();
	}

	private void updateView() {
		ResultSet rs = null;
		rs = drinkModel.drinkDao.getDrinkResultSet();
		if (rs != null) {
			tableDrink.setModel(DbUtils.resultSetToTableModel(rs));
		} else {
			JOptionPane.showMessageDialog(null, "DrinkView rs fault");
		}
	}

	private void initConponents() {
		buttonPanel = new JPanel();
		textPanel = new JPanel();
		tablePanel = new JPanel();

		// ***ButtonPanel
		add(buttonPanel);
		newB = new JButton(NEW); // new button
		newB.setActionCommand(NEW);
		newB.addActionListener(buttonActionListener);
		buttonPanel.add(newB);

		updateB = new JButton(UPDATE);
		updateB.setActionCommand(UPDATE);
		updateB.addActionListener(buttonActionListener);
		buttonPanel.add(updateB);

		deleteB = new JButton(DELETE);
		deleteB.setActionCommand(DELETE);
		deleteB.addActionListener(buttonActionListener);
		buttonPanel.add(deleteB);

		saveB = new JButton(SAVE);
		saveB.setActionCommand(SAVE);
		saveB.addActionListener(buttonActionListener);
		saveB.setEnabled(false);
		buttonPanel.add(saveB);

		cancelB = new JButton(CANCEL);
		cancelB.setActionCommand(CANCEL);
		cancelB.addActionListener(buttonActionListener);
		cancelB.setEnabled(false);
		buttonPanel.add(cancelB);

		buttonPanel.setLayout(new GridLayout(5, 1));
		buttonPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Drink Actions"));
		// ***end of ButtonPanel

		// ***TextPanel
		add(textPanel);
		JLabel label = new JLabel("Drink ID :");
		textPanel.add(label);
		drinkIdTF = new JTextField();
		textPanel.add(drinkIdTF);
		label = new JLabel("Drink Name :");
		textPanel.add(label);
		drinkNameTF = new JTextField();
		textPanel.add(drinkNameTF);
		label = new JLabel("Drink Description :");
		textPanel.add(label);
		drinkDescTF = new JTextField();
		textPanel.add(drinkDescTF);
		label = new JLabel("Drink Price :");
		textPanel.add(label);
		drinkPriceTF = new JTextField();
		textPanel.add(drinkPriceTF);

		textPanel.setLayout(new GridLayout(4, 2));
		textPanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Drink Detail"));
		// ***end of TextPanel

		// ***TablePanel
		add(tablePanel);

		// creating a default table model and override its isCellEditable
		// in order to disabling all the cells from edit from mouse click
		DefaultTableModel model = new DefaultTableModel();
		tableDrink = new JTable(model) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		// tableDrink = new JTable();
		tableDrink.addMouseListener(tableMouseAdapter);
		JScrollPane scrollPane = new JScrollPane(tableDrink);
		tablePanel.add(scrollPane);
		// tablePanel.add(tableDrink);
		tablePanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Drink Table"));
		// ***end of TablePanel
		edComponents(textPanel, false);// set disable textPanel components at
										// the beginning
	}// end initcomponents

	// sets enable or disable all components in a container
	private void edComponents(Container container, boolean b) {
		Component[] components = container.getComponents();
		for (Component component : components) {
			component.setEnabled(b);
			if (component instanceof Container) {
				edComponents((Container) component, b);
			}
		}
	}

	// set correct enable disable button Panel components
	private void edComponents(Container container, String cmd) {
		switch (cmd) {
		case NEW:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case UPDATE:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case DELETE:
			// handle delete
			// JOptionPane.showMessageDialog(null, "Delete button pressed");
			break;
		case SAVE:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		case CANCEL:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		}
	}

	class TableMouseAdapter extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (tableDrink.isEnabled()) {// only work when tableDrink is enabled
				System.out.println("mouse clicked on table");
				try {
					int row = tableDrink.getSelectedRow();
					dId = (int) tableDrink.getModel().getValueAt(row, 0);

					setTableRowToTextField(dId);
				} catch (Exception e2) {
					System.out.println("Mouse listener Exception!");
				}
			}
		}
	}

	class ButtonActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			switch (cmd) {
			case NEW:
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				System.out.println("NEWtbn in drink view");
				isNewButton = !isNewButton;// set isNewButton opposite state
				emptyTextField();
				break;
			case UPDATE:
				if (!drinkIdTF.getText().isEmpty()) {// only work if there is to
														// update
					b = !b;// if its true; set false....& vice versa
					edComponents(textPanel, b);// enable disable components in
												// container
					drinkIdTF.setEnabled(false);// shouldnt change drink id
												// number
					edComponents(tablePanel, !b);
					edComponents(buttonPanel, cmd);// enable disable components
													// in container
					System.out.println("UPDATEbtn in drink view ," + b);
					isUpdateButton = !isUpdateButton;// set isUpdateButton
														// opposite state
				} else {
					JOptionPane.showMessageDialog(null,
							"please click first on some item!!");
				}
				break;

			case DELETE:
				if (!drinkIdTF.getText().isEmpty()) {// only work if there is to
														// delete
					edComponents(buttonPanel, cmd);// enable disable components
													// in container and handle
													// delete button
					System.out.println("DELETEbtn in employee view");
					drinkModel.drinkDao.deleteDrink(drinkIdTF.getText());
					updateView();
				} else {
					JOptionPane.showMessageDialog(null,
							"please click first on some item!!");
				}
				break;

			case SAVE:
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				// decide action for save button (NewButton or UpdateButton ?)
				if (isUpdateButton) {
					isUpdateButton = !isUpdateButton;
					System.out.println("save for Update Button");
					drinkModel.drinkDao.updateDrink((drinkIdTF.getText()),
							(drinkNameTF.getText()), (drinkDescTF.getText()),
							(drinkPriceTF.getText()));
					updateView();
				}
				// decide action for save button (NewButton or UpdateButton ?)
				if (isNewButton) {
					isNewButton = !isNewButton;
					System.out.println("save for New Button");
					drinkModel.drinkDao.newDrink((drinkIdTF.getText()),
							(drinkNameTF.getText()), (drinkDescTF.getText()),
							(drinkPriceTF.getText()));
					updateView();
				}
				break;

			case CANCEL:
				isNewButton = !isNewButton;
				isUpdateButton = !isUpdateButton;
				b = !b;// if its true; set false....& vice versa
				edComponents(textPanel, b);// enable disable components in
											// container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);// enable disable components in
												// container
				System.out.println("CANCELbtn in admin view");
				if (dId != 0) {
					setTableRowToTextField(dId);
				}
				break;

			default:
				break;
			}
		}
	}// end ButtonActionListener class

	private void setTableRowToTextField(int dId) {
		Drink tmpDrink = drinkModel.drinkDao.getDrink(dId);
		if (tmpDrink != null) {
			System.out.println(dId);

			drinkIdTF.setText(Integer.toString(tmpDrink.getDrinkId()));
			drinkNameTF.setText(tmpDrink.getDrinkName());
			drinkDescTF.setText(tmpDrink.getDrinkDesc());
			drinkPriceTF.setText(Double.toString(tmpDrink.getDrinkPrice()));
		}
	}

	private void emptyTextField() {
		drinkIdTF.setText("");
		drinkNameTF.setText("");
		drinkDescTF.setText("");
		drinkPriceTF.setText("");
	}
}
